package com.example.myedittext;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.net.Socket;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    private EditText ip;
//    public String ipString = "192.168.3.32";   // 192.168.31.71服务器端ip   10.3.183.84
//    public String port1 = "8082";  //7890服务器端口

    // 验证 IP 地址格式
    public static boolean isValidIP(String ip) {
        String ipPattern = "^192\\.168\\.(\\d{1,3})\\.(\\d{1,3})$";
        return Pattern.matches(ipPattern, ip);
    }

    // 验证端口号格式
    public static boolean isValidPort(String port) {
        try {
            int portNumber = Integer.parseInt(port);
            return portNumber >= 0 && portNumber <= 65535 && port.length() <= 5 && port.matches("\\d{4}");
        } catch (NumberFormatException e) {
            return false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ip =findViewById(R.id.ip);
        EditText port=findViewById(R.id.port);
        Button btn=findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String iptext = ip.getText().toString();//获取ip内容
                String porttext = port.getText().toString();;//获取port内容
                if(iptext.equals("") && porttext.equals("")){
                    Toast.makeText(MainActivity.this, "请输入ip地址和端口号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(iptext.equals("")){
                    Toast.makeText(MainActivity.this, "请输入ip地址", Toast.LENGTH_SHORT).show();
                    System.out.println("请输入ip");
                    return;
                }
                if(porttext.equals("")){
                    Toast.makeText(MainActivity.this, "请输入端口号", Toast.LENGTH_SHORT).show();
                    System.out.println("请输入端口号");
                    return;
                }
//                if (iptext.equals(ipString)&&porttext.equals(port1))
                if (isValidIP(iptext)) {
                    if(isValidPort(porttext)){
                      Intent intent = new Intent();  //进入第二页
                      intent.putExtra("IP", iptext);
                      intent.putExtra("PORT", porttext);
                      intent.setClass(MainActivity.this,MainActivity2.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(MainActivity.this, "端口号格式错误", Toast.LENGTH_SHORT).show();
                        System.out.println("端口格式错误");
                    }
                } else {
                    Toast.makeText(MainActivity.this, "IP格式错误", Toast.LENGTH_SHORT).show();
                    System.out.println("IP格式错误");
                }
            }
        });
    }
}


